﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Tequila.Phone.Controls
{
    /// <summary>
    /// Image <see cref="Button">button</see>.
    /// </summary>
    public class ImageButton : Button
    {
        /// <summary>
        /// <see cref="DependencyProperty">Dependency Property</see>.
        /// </summary>
        public static readonly DependencyProperty EnabledImageSourceProperty =
            DependencyProperty.Register("EnabledImageSource",
                                        typeof (ImageSource),
                                        typeof (ImageButton),
                                        new PropertyMetadata(null));

        /// <summary>
        /// <see cref="DependencyProperty">Dependency Property</see>.
        /// </summary>
        public static readonly DependencyProperty DisabledImageSourceProperty =
            DependencyProperty.Register("DisabledImageSource",
                                        typeof (ImageSource),
                                        typeof (ImageButton),
                                        new PropertyMetadata(null));

        /// <summary>
        /// <see cref="DependencyProperty">Dependency Property</see>.
        /// </summary>
        public static readonly DependencyProperty ClickedImageSourceProperty =
            DependencyProperty.Register("ClickedImageSource",
                                        typeof (ImageSource),
                                        typeof (ImageButton),
                                        new PropertyMetadata(null));

        /// <summary>
        /// <see cref="DependencyProperty">Dependency Property</see>.
        /// </summary>
        public static readonly DependencyProperty IsVisibleProperty =
            DependencyProperty.Register("IsVisible",
                                        typeof (bool),
                                        typeof (ImageButton),
                                        new PropertyMetadata(false, OnIsVisiblePropertyChanged));

        private static void OnIsVisiblePropertyChanged(
            DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is FrameworkElement)) return;

            (d as FrameworkElement).Visibility = (bool) e.NewValue ? Visibility.Visible : Visibility.Collapsed;
        }

        /// <summary>
        /// Initializes new instance of class.
        /// </summary>
        public ImageButton()
        {
            DefaultStyleKey = typeof (ImageButton);
        }

        /// <summary>
        /// Gets or sets visibility.
        /// </summary>
        public bool IsVisible
        {
            get { return (bool) GetValue(IsVisibleProperty); }
            set { SetValue(IsVisibleProperty, value); }
        }

        /// <summary>
        /// Gets or sets enabled <see cref="ImageSource">source</see>.
        /// </summary>
        public ImageSource EnabledImageSource
        {
            get { return GetValue(EnabledImageSourceProperty) as ImageSource; }
            set { SetValue(EnabledImageSourceProperty, value); }
        }

        /// <summary>
        /// Gets or sets disabled <see cref="ImageSource">source</see>.
        /// </summary>
        public ImageSource DisabledImageSource
        {
            get { return GetValue(DisabledImageSourceProperty) as ImageSource; }
            set { SetValue(DisabledImageSourceProperty, value); }
        }

        /// <summary>
        /// Gets or sets clicked <see cref="ImageSource">source</see>.
        /// </summary>
        public ImageSource ClickedImageSource
        {
            get { return GetValue(ClickedImageSourceProperty) as ImageSource; }
            set { SetValue(ClickedImageSourceProperty, value); }
        }
    }
}